//
//  MerchandisePartialView.m
//  ios-supplier
//
//  Created by feynmanyuan on 15/4/19.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "MerchandisePartialView.h"
#import "JFBMerchandiseDS.h"
#import "JFBPhoneCallHelper.h"

@interface MerchandisePartialView(){
    NSUInteger _merchandisePriceWidth;
    NSUInteger _detailViewHeight;
    UIButton *_call;
}
@property (weak, nonatomic) IBOutlet UIView *orderDetailView;

@property (weak, nonatomic) IBOutlet UILabel *constTotalPriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *constPromotionLabel;
@property (weak, nonatomic) IBOutlet UILabel *constCarriageLabel;
@property (weak, nonatomic) IBOutlet UILabel *constGJLabel;
@property (weak, nonatomic) IBOutlet UILabel *constSPLabel;

@property (weak, nonatomic) IBOutlet UILabel *totalCountLabel;

@property (weak, nonatomic) IBOutlet UILabel *totalPriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *promotionLabel;
@property (weak, nonatomic) IBOutlet UILabel *carriageLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *constSSLabel;

@property (weak, nonatomic) IBOutlet UILabel *balance_amount;
@property (weak, nonatomic) IBOutlet UILabel *balance;

//@property (nonatomic, strong) JFBDrawLineHelper *drawLineHelper;

@end

@implementation MerchandisePartialView

- (void)setOrder:(JFBOrderDS*)order{
    

    self.drawLineHelper = [[JFBDrawLineHelper alloc]init];
    
    self.orderDetailView = self;
    
    CGSize priceSpacingSize = [@"¥ 0.0" sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    _merchandisePriceWidth = priceSpacingSize.width + kOrderCellSpacing * 2;
    
    CGFloat zengPinPrice = [self drawOrderDetailView:order];
    
    CGSize minPriceSize = [@"¥ 999.0" sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    CGSize constMinPriceSize = [@"配送费：" sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    // 总价
    CGFloat totalPriceY = _detailViewHeight + kOrderCellSpacing;
    CGFloat totalPriceX = self.bounds.size.width - kOrderCellSpacing - minPriceSize.width;
    CGRect totalPriceRect = CGRectMake(totalPriceX, totalPriceY, minPriceSize.width, minPriceSize.height);
    self.totalPriceLabel.frame = totalPriceRect;
    self.totalPriceLabel.text = [NSString stringWithFormat:@"¥ %.01f", [order.originMoney doubleValue] + zengPinPrice];
    
    CGRect constTotalPriceRect = CGRectMake(totalPriceX - kOrderCellSpacing - constMinPriceSize.width, totalPriceY, constMinPriceSize.width, constMinPriceSize.height);
    self.constTotalPriceLabel.frame = constTotalPriceRect;
    
    // 优惠
    CGFloat promotionPriceY = totalPriceRect.origin.y + totalPriceRect.size.height + kOrderCellSpacing;
    CGFloat promotionPriceX = self.bounds.size.width - kOrderCellSpacing - minPriceSize.width;
    CGRect promotionPriceRect = CGRectMake(promotionPriceX, promotionPriceY, minPriceSize.width, minPriceSize.height);
    
    self.promotionLabel.frame = promotionPriceRect;
    self.promotionLabel.text = [NSString stringWithFormat:@"- ¥ %.01f", [order.promotionMoney doubleValue] + zengPinPrice];
    
    CGRect constPromotionPriceRect = CGRectMake(promotionPriceX - kOrderCellSpacing - constMinPriceSize.width, promotionPriceY, constMinPriceSize.width, constMinPriceSize.height);
    self.constPromotionLabel.frame = constPromotionPriceRect;
    
    // 礼金卡金额
    CGFloat carriageY = promotionPriceRect.origin.y + promotionPriceRect.size.height + kOrderCellSpacing;
    CGFloat carriageX = self.bounds.size.width - kOrderCellSpacing - minPriceSize.width;
    CGRect carriageRect = CGRectMake(carriageX, carriageY, minPriceSize.width, minPriceSize.height);
    self.balance_amount.text = [NSString stringWithFormat:@"- ¥ %@", [NSString decimalToString:order.balance_amount]];
    self.balance_amount.frame = carriageRect;
    
    // 礼金卡：
    CGRect constCarriageRect = CGRectMake(carriageX - kOrderCellSpacing - constMinPriceSize.width, carriageY, constMinPriceSize.width, constMinPriceSize.height);
    self.balance.frame = constCarriageRect;
    CGFloat carriageY1 = CGRectGetMaxY(self.balance.frame) + kOrderCellSpacing;
    CGRect carriageRect1 = CGRectMake(carriageX, carriageY1, minPriceSize.width, minPriceSize.height);
    
    // 配送 金额
    self.carriageLabel.frame = carriageRect1;
    self.carriageLabel.text = [NSString stringWithFormat:@"¥ %@", [NSString decimalToString:order.carriageMoney]];
    
    // 配送费
    CGRect constCarriageRect1 = CGRectMake(carriageX - kOrderCellSpacing - constMinPriceSize.width, carriageY1, constMinPriceSize.width, constMinPriceSize.height);
    self.constCarriageLabel.frame = constCarriageRect1;
    
    NSUInteger nextLevelHeight = CGRectGetMaxY(self.carriageLabel.frame) + kOrderCellSpacing;
    
    [self.drawLineHelper addHorizontalLine:kOrderCellSpacing y:nextLevelHeight color:LineColorDefault width:self.bounds.size.width - kOrderCellSpacing * 2];
    
    NSUInteger nextHeight = nextLevelHeight + (kOrderCellHeaderHeight - self.constGJLabel.bounds.size.height) / 2;
    
    CGRect gjRect = CGRectMake(kOrderCellSpacing, nextHeight, self.constGJLabel.bounds.size.width,  self.constGJLabel.bounds.size.height);
    
    self.constGJLabel.frame = gjRect;
    CGRect totalCountRect = CGRectMake(kOrderCellSpacing + 2 + gjRect.size.width , nextHeight, self.totalCountLabel.bounds.size.width,  self.totalCountLabel.bounds.size.height);
    self.totalCountLabel.frame = totalCountRect;
    
    CGRect constSPRect = CGRectMake(kOrderCellSpacing + 2 + gjRect.size.width + totalCountRect.size.width , nextHeight, self.constSPLabel.bounds.size.width,  self.constSPLabel.bounds.size.height);
    self.constSPLabel.frame = constSPRect;
    self.totalCountLabel.text = [NSString stringWithFormat:@"%lu", (unsigned long)order.totalCount];
    
    self.priceLabel.text = [NSString stringWithFormat:@"¥ %@", [NSString decimalToString:order.money]];
    CGSize priceSize = [self.priceLabel.text sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH1]}];
    CGFloat priceX = self.bounds.size.width - kOrderCellSpacing * 3 - priceSize.width;
    CGFloat priceY = nextLevelHeight + (kOrderCellHeaderHeight - priceSize.height) / 2;
    CGRect priceRect = CGRectMake(priceX, priceY, priceSize.width + kOrderCellSpacing * 2,  priceSize.height);
    self.priceLabel.frame = priceRect;
//    self.priceLabel.text = price;
    
    CGRect ssRect = CGRectMake(priceX - self.constSSLabel.bounds.size.width, constSPRect.origin.y, self.constSSLabel.bounds.size.width, self.constSSLabel.bounds.size.height);
    self.constSSLabel.frame = ssRect;
    
    self.height = nextLevelHeight + kOrderCellHeaderHeight;
    
}

// 订单商品列表
-(CGFloat)drawOrderDetailView:(JFBOrderDS *)order{
    CGFloat zengPinPrice = 0;
    NSUInteger height = 0;
    NSUInteger width = self.bounds.size.width - kOrderCellSpacing * 2 - _merchandisePriceWidth - 30 - kOrderCellImageSize;
    
    for( JFBMerchandiseDS *ds in order.merchandiseList ) {
        
        NSUInteger itemCellHeight = 0;
        
        UILabel *merchandiseName = [[UILabel alloc] init];
        merchandiseName.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
        merchandiseName.lineBreakMode = NSLineBreakByCharWrapping;
        merchandiseName.numberOfLines = 0;
        [merchandiseName setTextColor:TextColorLight];
        merchandiseName.font = [UIFont systemFontOfSize:TextFontSizeH4];
        merchandiseName.text = ds.name;
        
        CGSize size;
        CGFloat offX;
//        if (ds.type == 1) // 赠品价格为0
//        {
//            UILabel *lblZengPin = [[UILabel alloc] initWithFrame:CGRectMake(kOrderCellSpacing * 2 + kOrderCellImageSize, (itemCellHeight - 15) / 2, 35, 15)];
//            [lblZengPin setTextColor: [UIColor whiteColor]];
//            [lblZengPin setTextAlignment:NSTextAlignmentCenter];
//            [lblZengPin setText:@"赠品"];
//            [lblZengPin setFont:[UIFont systemFontOfSize:13]];
//            [lblZengPin setBackgroundColor: [UIColor redColor]];
//            [_orderDetailView addSubview:lblZengPin];
//            
//            offX = CGRectGetMaxX(lblZengPin.frame) + 5;
//            size = [merchandiseName sizeThatFits:CGSizeMake(width - 35 - 5, MAXFLOAT)];
//            itemCellHeight = size.height + kOrderCellSpacing * 2;
//            
//            if( itemCellHeight < kOrderCellImageSize + kOrderCellSpacing * 2 ) {
//                itemCellHeight = kOrderCellImageSize + kOrderCellSpacing * 2;
//            }
//            
//            merchandiseName.frame =CGRectMake(offX, height + (itemCellHeight - size.height) / 2, width - 35 - 5, size.height);
//            lblZengPin.y = merchandiseName.y;
//            zengPinPrice += ds.marketPrice.doubleValue * ds.count;
//        }
//        else
//        {
//            offX = kOrderCellSpacing * 2 + kOrderCellImageSize;
//            
//            size = [merchandiseName sizeThatFits:CGSizeMake(width, MAXFLOAT)];
//            itemCellHeight = size.height + kOrderCellSpacing * 2;
//            
//            if( itemCellHeight < kOrderCellImageSize + kOrderCellSpacing * 2 ) {
//                itemCellHeight = kOrderCellImageSize + kOrderCellSpacing * 2;
//            }
//            
//            merchandiseName.frame =CGRectMake(offX, height + (itemCellHeight - size.height) / 2, width, size.height);
//        }
        
        [_orderDetailView addSubview:merchandiseName];
        
        
        CGRect frame = CGRectMake(kOrderCellSpacing, height + (itemCellHeight - kOrderCellImageSize) / 2, kOrderCellImageSize, kOrderCellImageSize);
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
        imageView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
        imageView.layer.borderColor = ImageBoderColorDefault.CGColor;
        imageView.layer.borderWidth = 1;
        [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://wup.jufangbian.com/upload/product_image/product_150/%lu.jpg", (unsigned long)ds.mpURL]] placeholderImage:[UIImage imageNamed:ImageDefaultPlaceholder]];
        NSLog(@"image-url:%@", [NSString stringWithFormat:@"http://wup.jufangbian.com/upload/product_image/product_150/%lu.jpg", (unsigned long)ds.mpURL]);
        [_orderDetailView addSubview:imageView];
        
        
        UILabel *priceLabel = [[UILabel alloc] init];
        priceLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
        
        NSString *price;
//        if (ds.type == 1) // 赠品价格为0
//        {
//            price = @"¥ 0.0";
//        }
//        else
//        {
            price = [NSString stringWithFormat:@"¥ %@", [NSString decimalToString: ds.marketPrice]];
//        }
        
        priceLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
        priceLabel.textColor = TextColorLight;
        CGSize priceSize = [price sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
        
        UILabel *itemCountLabel = [[UILabel alloc]init];
        itemCountLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
        NSString *itemCount = [NSString stringWithFormat:@"x %lu", (NSInteger)ds.standard];
        itemCountLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
        itemCountLabel.textColor = TextColorLight;
        CGSize itemCountSize = [itemCount sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
        
        CGFloat itemCountX = self.bounds.size.width - kOrderCellSpacing - itemCountSize.width;
        
        CGFloat vs = (itemCellHeight - priceSize.height - itemCountSize.height) / 3;
        
        CGFloat priceY = height + vs;
        CGFloat itemCountY = height + vs * 2 + priceSize.height;
        
        CGFloat priceX = self.bounds.size.width - kOrderCellSpacing - priceSize.width;
        CGRect priceRect = CGRectMake(priceX, priceY, priceSize.width, priceSize.height);
        priceLabel.frame = priceRect;
        priceLabel.text = price;
        
        CGRect itemCountRect = CGRectMake(itemCountX, itemCountY, itemCountSize.width, itemCountSize.height);
        itemCountLabel.text = itemCount;
        itemCountLabel.frame = itemCountRect;
        [self.orderDetailView addSubview:itemCountLabel];
        
        [self.orderDetailView addSubview:priceLabel];
        
        [self.drawLineHelper addverticalLine:self.bounds.size.width - _merchandisePriceWidth - 30 y:height + kOrderCellSpacing color:LineColorDefault height:itemCellHeight - kOrderCellSpacing * 2 isDash:true];
        
        height += itemCellHeight;
        
        [self.drawLineHelper addHorizontalLine:kOrderCellSpacing y:height color:LineColorDefault width:self.bounds.size.width - kOrderCellSpacing * 2];
        
    }
    _detailViewHeight = height;
    return zengPinPrice;
}


@end
